if Player == nil then
    Player = {}
    Player.__index = Player
end

function Player:new( o )
    o = o or {}
    setmetatable( o, Player )
    return o
end


function Player:Init()
    Player.playerList={}


end


function Player:InitPlayer(playerId,isBot)
    print("[Player:InitPlayer]",playerId,isBot)
    local playerInfo={}
    playerInfo.playerId=playerId
    playerInfo.isOnline=true
    playerInfo.isBot=isBot
    playerInfo.color=nil
    playerInfo.teamId=PlayerResource:GetTeam(playerId)
    --playerInfo.isBot=true
    playerInfo.steamId=tostring(PlayerResource:GetSteamID(playerId))
    playerInfo.steamAccountId = tostring(PlayerResource:GetSteamAccountID(playerId))
    playerInfo.gold=0
    playerInfo.life=100
    playerInfo.isAlive=true
    playerInfo.rank=0
    playerInfo.grade="1"
    playerInfo.kills=0
    playerInfo.deaths=0
    playerInfo.heroLv=1
    playerInfo.heroName=nil
    playerInfo.deathTime=0
    playerInfo.playerName = tostring(PlayerResource:GetPlayerName(playerId))
    playerInfo.modifyRank=nil
    Player.playerList[playerId]=playerInfo
end

function Player:StartSync()
    Timers:CreateTimer(0,function ()
        CustomNetTables:SetTableValue(NetTableNames.player_info, "all", Player:GetSyncTable())
        return 1
    end)
end

function Player:GetSyncTable()
    local playerInfoTable = {}
    for playerId, playerInfo in pairs(Player.playerList) do
        local abilityNames={}
        local hero=Hero.heroList[playerId]
        if(hero~=nil) then
            for i=0,GameConfig.abilitiesSlotCount-1 do
                table.insert(abilityNames,hero:GetAbilityByIndex(i):GetAbilityName())
            end
            playerInfo.heroLv=hero:GetLevel()
            playerInfo.kills=hero:GetKills()
            playerInfo.deaths=hero:GetDeaths()
            playerInfo.teamId=hero:GetTeam()
            playerInfo.heroName=hero:GetUnitName()
            if table.contains(AllTeamIndex,playerInfo.teamId) then
                playerInfo.color=ColorHelper:TableToCssColor(GameRules.G.teamColorList[playerInfo.teamId])
            end
            local courier= PlayerResource:GetNthCourierForTeam(0, hero:GetTeamNumber())
            if courier~=nil then
                if playerInfo.isAlive==true then
                    playerInfo.life=courier:GetHealth()
                end
            end
            local gradeNum=math.floor(playerInfo.rank/500)+1
            if gradeNum==8 then
                playerInfo.grade="7a"
            elseif gradeNum==9 then
                playerInfo.grade="7b"
            elseif gradeNum==10 then
                playerInfo.grade="7c"
            elseif gradeNum==11 then
                playerInfo.grade="8"
            elseif gradeNum==12 then
                playerInfo.grade="8b"
            elseif gradeNum>=13 then
                playerInfo.grade="8c"
            else
                playerInfo.grade=tostring(gradeNum)
            end
        end

        local tableData = {
            playerId = playerId,
            playerName = playerInfo.playerName,
            steamId = playerInfo.steamId,
            teamId = playerInfo.teamId,
            kills=playerInfo.kills,
            deaths=playerInfo.deaths,
            color = playerInfo.color,
            isOnline = playerInfo.IsOnline,
            isBot = playerInfo.isBot,
            isAlive=playerInfo.isAlive,
            gold = playerInfo.gold,
            life = playerInfo.life,
            rank = playerInfo.rank,
            abilityNames=abilityNames,
            heroName=playerInfo.heroName,
            heroLv=playerInfo.heroLv,
            grade = playerInfo.grade,
            modifyRank=playerInfo.modifyRank
        }
        --table.insert(playerInfoTable, tableData)
        --table.insert(playerInfoTable, tableData)
        --table.insert(playerInfoTable, tableData)
        --table.insert(playerInfoTable, tableData)
        --table.insert(playerInfoTable, tableData)
        --table.insert(playerInfoTable, tableData)
        --table.insert(playerInfoTable, tableData)
        table.insert(playerInfoTable, tableData)
    end
    table.sort(playerInfoTable, function(a, b)
        if a.life~=0 and a.life==b.life then
            return (a.kills-a.deaths)>(b.kills-b.deaths)
        end

        if a.life==b.life and a.life==0 then
            return (Player.playerList[a.playerId].deathTime)>(Player.playerList[b.playerId].deathTime)
        end
        return a.life>b.life
    end)
    return playerInfoTable
end

function Player:FindEmptyTeamForPlayer(playerId)
    local assignTeam = PlayerResource:GetCustomTeamAssignment(playerId)
    if(table.containsKey(GameRules.G.teamColorList, assignTeam)) then
        return assignTeam
    end
    local teamAssignedTable = {}
    for _, playerInfo in pairs(Player.playerList) do
        local player = PlayerResource:GetPlayer(playerInfo.playerId)
        if(player ~= nil and player:IsNull() == false) then
            local teamId = player:GetTeam()
            if(table.contains(AllTeamIndex, teamId)) then
                table.insert(teamAssignedTable, teamId)
            end
        end
    end
    for _, value in pairs(AllTeamIndex) do
        if(table.contains(teamAssignedTable, tonumber(value)) == false) then
            return value
        end
    end
    return DOTA_TEAM_NOTEAM
end

function Player:UpdatePlayerColor( nPlayerID )
    if not PlayerResource:HasSelectedHero( nPlayerID ) then
        return
    end

    local hero = PlayerResource:GetSelectedHeroEntity( nPlayerID )
    if hero == nil then
        return
    end

    local teamID = PlayerResource:GetTeam( nPlayerID )
    local color = GameMode:ColorForTeam( teamID )
    PlayerResource:SetCustomPlayerColor( nPlayerID, color[1], color[2], color[3] )
end

function Player:GetPlayerPostData(player)
    local postData={}
    postData.isBot=player.isBot
    postData.steamId=player.steamId
    postData.playerId=player.playerId
    return postData
end